<xsl:stylesheet version="1.0" xmlns:asp="remove" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"></xsl:output>  
  <xsl:template match="/">
 <xsl:text disable-output-escaping="yes">&lt;</xsl:text>?php

include ('App.php');
include ('Collection.php');

class <xsl:value-of select="/Class/Name"/> {
	//  Region:  DB / DB Fields
	const __TBL = "<xsl:value-of select="/Class/View/Name" />";
	<xsl:for-each select="Class/View/Columns/Column">	const __FLD_<xsl:value-of select="ConstName"/> = "<xsl:value-of select="Name"/>";	
  </xsl:for-each>
      private $conn;
	//  Region:  DB / DB Fields End

	//  Region:  Properties
	<xsl:for-each select="Class/View/Columns/Column">	private $<xsl:value-of select="VBName" />;
  </xsl:for-each>	
      private $isLoaded;

	function __construct(<xsl:for-each select="Class/View/Columns/Column">$<xsl:value-of select="VBName" /> = ""<xsl:if test="count(following-sibling::*[Name])!=0">, </xsl:if></xsl:for-each>) {
		$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>conn 		=		App::getDB();
		<xsl:for-each select="Class/View/Columns/Column">$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text><xsl:value-of select="VBName" /> = $<xsl:value-of select="VBName" />;
    </xsl:for-each>	
	}

	public function returnTable(){
		return 	self::__TBL;
	}

// Region: Standard Functions
	public function populate($PrimKeyValue){
		App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Table(self::__TBL);
		<xsl:for-each select="Class/View/Columns/Column">		<xsl:if test="IsPrimaryKey='True'">		App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Where(self::__FLD_<xsl:value-of select="ConstName"/>,"=",$PrimKeyValue);</xsl:if>
		</xsl:for-each>
		$sql = App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SELECTStatement();
		$row = App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>GetDataRow($sql);
		$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>getRow($row);
	}
	private function getRow($row){
		<xsl:for-each select="Class/View/Columns/Column">
		$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text><xsl:value-of select="VBName" /> = $row[self::_FLD_<xsl:value-of select="ConstName"/>];</xsl:for-each>
		$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>isLoaded = true;
	}

	public function save() {
		App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Table(self::__TBL);
		<xsl:for-each select="Class/View/Columns/Column">		App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Field(self::__FLD_<xsl:value-of select="ConstName"/>,$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text><xsl:value-of select="VBName" />);
		</xsl:for-each>
		if ($this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>isLoaded === true) {
			<xsl:for-each select="Class/View/Columns/Column"><xsl:if test="IsPrimaryKey='True'">			App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Where(self::__FLD_<xsl:value-of select="ConstName"/>,"=",$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text><xsl:value-of select="VBName" />);      </xsl:if>
      </xsl:for-each>
			$SQL = App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>UPDATEStatement();
		} else {
			$SQL = App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>INSERTStatement();
		}
		App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Execute($SQL);
	}
}

class <xsl:value-of select="/Class/Name"/>Collection{

	private $Class;
	private $__TBL;
	public $Collection;
	
	function __construct() {
		$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Collection 			= 	new Collection;
		$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Class				= 	new UserClass;
		$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>__TBL				= 	$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Class-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>returnTable();
	}
	
	public  function __toString(){
		return $this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Class-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>UserName;
	}
	
	public function Retrieve(){
		try{
			App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Table($this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>__TBL);
			$sql = App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SqlBuilder-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>SELECTStatement();
			$DataTable = App::$DAL-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>getArrayFromDB($sql);
			foreach ($DataTable as $row) {
				$this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Collection-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>addItem(
					new UserClass(
          <xsl:for-each select="Class/View/Columns/Column">            $row['<xsl:value-of select="ConstName"/>']<xsl:if test="count(following-sibling::*[Name])!=0">, 
          </xsl:if>
            </xsl:for-each>
					),
          <xsl:for-each select="Class/View/Columns/Column"> <xsl:if test="IsPrimaryKey='True'">	$row['<xsl:value-of select="ConstName"/>']</xsl:if></xsl:for-each>
				);
			}
		}catch (Exception $e){
			App::$Logger-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Log(LogType::Error,$e-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>getMessage());
		}
	}

	public function getItemByKey($Key){
		try{
			return $this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Collection-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>getItem($Key);
		}catch (Exception $e){
			App::$Logger-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Log(LogType::Error,$e-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>getMessage());
		}
	}
	
	public function Count(){
		try{
			return $this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Collection-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>length();
		}catch (Exception $e){
			App::$Logger-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Log(LogType::Error,$e-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>getMessage());
		}
	}
	
	public function RemoveItemByKey($Key){
		try{
			return $this-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Collection-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>removeItem($key);
		}catch (Exception  $e){
			App::$Logger-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>Log(LogType::Error,$e-<xsl:text disable-output-escaping="yes">&gt;</xsl:text>getMessage());
		}
	}
}
?<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
  </xsl:template>
</xsl:stylesheet>